package net.hn.hnms.biz.meeting.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.meeting.domain.MeetingRoom;
import net.hn.hnms.biz.meeting.domain.ScheduleMining;
import net.hn.hnms.biz.meeting.mapper.ScheduleMiningMapper;
import net.hn.hnms.biz.meeting.request.MeetingRoomReq;
import net.hn.hnms.biz.meeting.request.ScheduleMiningReq;
import net.hn.hnms.biz.meeting.service.MeetingRoomService;
import net.hn.hnms.biz.meeting.service.ScheduleMiningService;
import net.hn.hnms.biz.meeting.vo.MeetingRoomVo;
import net.hn.hnms.biz.meeting.vo.ScheduleMiningVo;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import net.hn.hnms.sys.common.satoken.utils.LoginHelper;
import net.hn.hnms.sys.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ScheduleMiningServiceImpl extends BaseService<ScheduleMining, ScheduleMiningVo> implements ScheduleMiningService {

    @Autowired
    private ScheduleMiningMapper scheduleMiningMapper;


    @Override
    public ResponsePage<ScheduleMiningVo> selectPageList(ScheduleMiningReq req, PageQuery pageQuery) {
        LoginUser loginUserInfo = getLoginUserInfo();
        req.setUserId(loginUserInfo.getUserId());
        Page<ScheduleMining> page = scheduleMiningMapper.selectPage(pageQuery.build(), buildQuery(req).orderByDesc(ScheduleMining::getCreateTime));
        List<ScheduleMiningVo> list = page.getRecords().stream().map(o -> convert(o,ScheduleMiningVo.class)).collect(Collectors.toList());
        return new ResponsePage<>(list, page.getTotal());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDeleteByIds(List<String> idList) {
        scheduleMiningMapper.updateDeleteByIds(idList);
    }

    @Override
    public int insert(ScheduleMiningReq meetingRoomReq) {
        ScheduleMining insertObj = new ScheduleMining();
        LoginUser loginUserInfo = getLoginUserInfo();
        meetingRoomReq.setUserId(loginUserInfo.getUserId());
        BeanUtil.copyProperties(meetingRoomReq, insertObj);
        return scheduleMiningMapper.insert(insertObj);
    }

    @Override
    public void updateById(ScheduleMiningReq meetingRoomReq) {
        ScheduleMining updateObj = new ScheduleMining();
        LoginUser loginUserInfo = getLoginUserInfo();
        meetingRoomReq.setUserId(loginUserInfo.getUserId());
        BeanUtil.copyProperties(meetingRoomReq, updateObj);
        int updateCount = scheduleMiningMapper.updateById(updateObj);
        if (updateCount == 0) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
    }

    @Override
    public List<ScheduleMiningVo> selectReservePageList(Date dateTime, String mineCode, String companyCode) {
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void changeStatus(String scheduleId, Integer state) {
        if (state == 1 || state == 0) {
            ScheduleMining meetingRoom = scheduleMiningMapper.selectById(scheduleId);
            if (meetingRoom == null) {
                throw new HuaNengException(Constants.FAIL, "数据不存在");
            }
            meetingRoom.setStatus(state);
            int res = scheduleMiningMapper.updateById(meetingRoom);
            if (res == 0) {
                throw new HuaNengException(Constants.FAIL, "数据不存在");
            }
            return;
        }
        throw new HuaNengException(Constants.FAIL, "传入的参数错误");

    }

    @Override
    public List<ScheduleMiningVo> selectList(ScheduleMiningReq planMining) {
        return scheduleMiningMapper.getSelectList(planMining.getMineCode(),planMining.getYm());
    }

    @Override
    public List<ScheduleMiningVo> selectListData(ScheduleMiningReq planMining) {
        return scheduleMiningMapper.selectListData(planMining.getMineCode(),planMining.getYm());
    }

    private LambdaQueryWrapper<ScheduleMining> buildQuery(ScheduleMiningReq req){
        return new LambdaQueryWrapper<ScheduleMining>()
                .and(o->o.eq(req.getMineCode()!=null, ScheduleMining::getMineCode, req.getMineCode()).or().eq(req.getCompanyCode()!=null, ScheduleMining::getCompanyCode, req.getCompanyCode()))

                .eq(StrUtil.isNotBlank(req.getUserId().toString()), ScheduleMining::getUserId, req.getUserId())
                .eq(req.getScheduleDate()!=null, ScheduleMining::getScheduleDate, req.getScheduleDate());
    }


    private LoginUser getLoginUserInfo() {
        LoginUser loginUser;
        try {
            loginUser = LoginHelper.getLoginUser();
        } catch (Exception e) {
            throw new HuaNengException(Constants.FAIL, "用户未登录,查询不到登录用户信息,无法获取日程信息");
        }
        return loginUser;
    }
}
